myApp_.onPageInit('account',function(page){
	ACCOUNT.page = 1;//翻页
	ACCOUNT.scroll_flag = false;//翻页触发装置
	ACCOUNT.last_page = 1;//最后一页
	
	ACCOUNT.trade_type = '';
	
	ACCOUNT.fund_info();
	ACCOUNT.get_trade_type_list();
});

/**
 * 账户对象
 */
ACCOUNT = {};

ACCOUNT.is_load = false;

ACCOUNT.page = 1;//翻页
ACCOUNT.scroll_flag = true;//翻页触发装置
ACCOUNT.last_page = 1;//最后一页

//账单类型
ACCOUNT.trade_type = '';

ACCOUNT.__INIT__=function(){
	ACCOUNT.jump();
}

ACCOUNT.jump = function(){
	mainView.router.load({
        url: "src/my_center/account/account.html"
    });
}

ACCOUNT.no_more = function(){
	$("#accoutdetail").append('<p class="index_order_bottom" style="width: 100%;background: #fff;">—— 没有更多信息了哦 ——</p>');
}

ACCOUNT.fund_info = function(){ 
	if (ACCOUNT.page > ACCOUNT.last_page) {
        return;
    }
	var url = url_('api/V4/trade');
	var data = {
		page: ACCOUNT.page
		,trade_type: ACCOUNT.trade_type
	};
	var success = function(d){
		if (d.code == 0) {
		  var r_data = d.data
		  var source = $("#accoutdetail").html();
		  $('#account_yue').html('¥ '+r_data.user_fund)
		  ACCOUNT.last_page = r_data.trade_data.last_page;
		  if (ACCOUNT.page == 1) {
            if (!source) {
                $.tpl({
                    elem: "#accoutdetail",
                    url: "src/my_center/account/account_fill.html",
                    context: r_data,
                    after:function(){
	                	$('#account_yue').html(r_data.user_fund)
	                	
	                	if (ACCOUNT.page > ACCOUNT.last_page || ACCOUNT.last_page == 0) { 
					  		ACCOUNT.no_more();
					    }
	                }
                });
            }
		  } else {
            $.tpl({
                elem: "#accoutdetail",
                append:true,
                url: "src/my_center/account/account_fill.html",
                context: r_data,
                after:function(){
                	$('#account_yue').html(r_data.user_fund)
                	
                	if (ACCOUNT.page > ACCOUNT.last_page || ACCOUNT.last_page == 0) { 
				  		ACCOUNT.no_more();
				    }
                }
            });
		  }
		  ACCOUNT.page++;
		  ACCOUNT.scroll_flag = true;
		}else if(d.code == 1011){
			my_tips_(d.msg,function(){
				go_("login");
			});
		} else{
			my_tips_(d.msg);
		}
	}
	request_(url,data,success)
}

//翻页
ACCOUNT.scroll = function (e) {
    var me = $(e);
    var viewH = me.height();
    var contentH = me.get(0).scrollHeight;
    var scrollTop = me.scrollTop();
    if (scrollTop / (contentH - viewH) >= 0.94) {
        if (ACCOUNT.scroll_flag) {
            ACCOUNT.scroll_flag = false;
            ACCOUNT.fund_info();
        }
    }
}

ACCOUNT.detail = function(){
	var popupHTML = '<div class="popup">'+
                    '<div class="content-block">'+
                      '<p>账单详情</p>'+
                      '<p><a href="#" class="close-popup">点击关闭</a></p>'+
                    '</div>'+
                  '</div>'
    myApp_.popup(popupHTML);
}

/**
 * 账单记录类型列表
 */
ACCOUNT.trade_type_list = '';

/**
 * 获取账单记录类型列表
 */
ACCOUNT.get_trade_type_list = function(){
	request_(
		url_('api/V4/trade_type_list')
		,{}
		,function(d){
			if(d.code == 0){
				ACCOUNT.trade_type_list = d.data;
			}else{
				my_tips_(d.msg);
			}
		}
	);
}

/**
 * 弹出账单类型筛选
 */
ACCOUNT.pop_select = function(){
	var types_tpl = '{{#each this}}'+
	      			'<li><a href="#" onclick="ACCOUNT.set_trade_type({{trade_type}})" class="list-button item-link">{{des}}</a></li>'+
	      			'{{/each}}';
    var types = tpl_(types_tpl,ACCOUNT.trade_type_list);
			
	
	var popoverHTML = '<div class="popover acount_select_popover" style="width: 100px">'+
	                    '<div class="popover-angle"></div>'+
	                        '<div class="popover-inner">'+
	                      		'<div class="list-block">'+
	                      			'<ul>' + types + '</ul>'+
	                      		'</div">'+
       					    '</div>'+
	                    '</div>'+
	                '</div>'
    myApp_.popover(popoverHTML,'.account_select_kind');
}

ACCOUNT.set_trade_type = function(trade_type){
	myApp_.closeModal('.acount_select_popover');
	
	$('#accoutdetail').html('');
	
	ACCOUNT.page = 1;//翻页
	ACCOUNT.scroll_flag = false;//翻页触发装置
	ACCOUNT.last_page = 1;//最后一页
	
	ACCOUNT.trade_type = trade_type;
	ACCOUNT.fund_info();
}
